package com.zzz.robot201.mvp.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.app.config.AppContent;
import com.zzz.robot201.di.component.DaggerFeedBackComponent;
import com.zzz.robot201.mvp.contract.FeedBackContract;
import com.zzz.robot201.mvp.model.api.bean.BaseBean;
import com.zzz.robot201.mvp.model.api.bean.PopuBean;
import com.zzz.robot201.mvp.presenter.FeedBackPresenter;
import com.zzz.robot201.mvp.ui.activity.LoginActivity;
import com.zzz.robot201.mvp.ui.adapter.PopuAdapter;
import com.zzz.robot201.utils.SpUtils;
import com.zzz.robot201.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:反馈
 * <p>
 * Created by MVPArmsTemplate on 07/09/2020 01:21
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class FeedBackFragment extends BaseFragment<FeedBackPresenter> implements FeedBackContract.View {

    @BindView(R.id.ll_feed_type)
    RelativeLayout llFeedType;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_feed_tittle)
    TextView tvFeedTittle;
    private List<PopuBean.DataBean> list = new ArrayList<>();

    public static FeedBackFragment newInstance() {
        FeedBackFragment fragment = new FeedBackFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerFeedBackComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_feed_back, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (mPresenter != null) mPresenter.getTypes();
    }


    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
//        checkNotNull(message);
//        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @OnClick({R.id.ll_feed_type, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_feed_type:
                showPopWindow(getView());
                break;
            case R.id.btn_submit:
                String token =(String) SpUtils.get(getActivity(), AppContent.SaveInfoKey.TOKEN, "");
                if (TextUtils.isEmpty(token)){
                    ToastUtil.showToast("请登录反馈");
                    startActivity(new Intent(getContext(), LoginActivity.class));
                    return;
                }
                String tittle = tvFeedTittle.getText().toString();
                String content = etInput.getText().toString();
                if (!TextUtils.isEmpty(content)) {
                    if (mPresenter != null) mPresenter.Feedback(tittle, content);
                } else {
                    ToastUtil.showToast("请输入反馈内容");
                }

                break;
        }
    }

    private PopupWindow popupWindow;

    private void showPopWindow(View view) {
        try {
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int width = llFeedType.getMeasuredWidth();
            popupWindow = new PopupWindow(view, width,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setFocusable(true);
            popupWindow.setOutsideTouchable(true);
            ColorDrawable cd = new ColorDrawable(0x00ffffff);// 背景颜色全透明
            popupWindow.setBackgroundDrawable(cd);
            int[] location = new int[2];
            llFeedType.getLocationOnScreen(location);
//            popupWindow.setAnimationStyle(R.style.popupwindow_animation_style);// 动画效果必须放在showAsDropDown()方法上边，否则无效
            backgroundAlpha(0.5f);// 设置背景半透明
            popupWindow.showAsDropDown(llFeedType);
            //popupWindow.showAtLocation(tv_pop, Gravity.NO_GRAVITY, location[0]+tv_pop.getWidth(),location[1]);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                    popupWindow = null;// 当点击屏幕时，使popupWindow消失
                    backgroundAlpha(1.0f);// 当点击屏幕时，使半透明效果取消
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 设置popupWindow背景半透明
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha;// 0.0-1.0
        getActivity().getWindow().setAttributes(lp);
    }

    /*
     * 得到popupwindow的View
     */
    public View getView() {
        View view = LayoutInflater.from(getActivity()).inflate(
                R.layout.popup_window, null);
        ListView lv_pop = (ListView) view.findViewById(R.id.lv_pop);
        PopuAdapter popuAdapter = new PopuAdapter(list, getActivity());
        lv_pop.setAdapter(popuAdapter);
        lv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tvFeedTittle.setText(list.get(position).getTypeName());
                popupWindow.dismiss();
            }
        });
        return view;
    }

    @Override
    public void getTypes(PopuBean code) {
        list.addAll(code.getData());
    }

    @Override
    public void Feedback(BaseBean baseBean) {
        ToastUtil.showToast(baseBean.getMsg());
    }
}
